<?php

namespace app\api\controller;

use app\common\business\MemberCard;
use app\common\business\Wechat;
use app\common\lib\Show;
use app\common\lib\Status;
use think\facade\Log;

class Login extends Base
{
    public function getUserInfo()
    {
        $code = $this->request->param('code', '', 'trim');
        try {
            $code = $this->mini_app->auth->session($code);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, "登录失败");
        }
        $data = [
            "openid" => $code['openid']
        ];

        if(mb_strlen($data['openid'])!='28'){
            return Show::json_show(Status::mini_error, "微信获取openid失败");
        }
        try {
            $wechat = (new Wechat())->WechatByOpenid($data);
        } catch (\Exception $e) {
            Log::error($e->getMessage()." ".$e->getFile()." ".$e->getLine());
            return Show::json_show(Status::mini_error, $e->getMessage());
        }

        if ($wechat) {
            if($wechat['usertype']>0){
                $cardinfo =(new MemberCard())->getMembercardInfo($wechat['usertype']);
                $wechat['usertypename'] = $cardinfo['title'];
                $wechat['quanyi'] = $cardinfo['quanyi'];
            }else{
                $wechat['usertypename'] = '';
                $wechat['quanyi'] = '';
            }
            $wechat['isopenmanager'] =1;
            return Show::json_show(Status::mini_normal, "ok", $wechat);
        } else {
            return false;
        }
    }

}